home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / contrib / pdcurs22 / samples / testcurs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-26  |  14.9 KB  |  641 lines

  1. /*
  2. ***************************************************************************
  3. * This file comprises part of PDCurses. PDCurses is Public Domain software.
  4. * You may use this code for whatever purposes you desire. This software
  5. * is provided AS IS with NO WARRANTY whatsoever.
  6. * Should this software be used in another application, an acknowledgement
  7. * that PDCurses code is used would be appreciated, but is not mandatory.
  8. *
  9. * Any changes which you make to this software which may improve or enhance
  10. * it, should be forwarded to the current maintainer for the benefit of 
  11. * other users.
  12. *
  13. * The only restriction placed on this code is that no distribution of
  14. * modified PDCurses code be made under the PDCurses name, by anyone
  15. * other than the current maintainer.
  16. * See the file maintain.er for details of the current maintainer.
  17. ***************************************************************************
  18. */
  19. /*
  20.  *
  21.  * This is a test program for the PDCurses screen package for IBM PC type
  22.  * machines.
  23.  * This program was written by John Burnell (johnb@kea.am.dsir.govt.nz)
  24.  *
  25.  *  wrs(5/28/93) -- modified to be consistent (perform identically) with either
  26.  *                  PDCurses or under Unix System V, R4
  27.  *
  28.  */
  29.  
  30. #ifdef PDCDEBUG
  31. char *rcsid_testcurs  = "$Id$";
  32. #endif
  33.  
  34. #include <stdio.h>
  35. #include <curses.h>
  36.  
  37. #if defined(XCURSES)
  38.     char *XCursesProgramName = "testcurs";
  39. #endif
  40.  
  41. #ifdef __STDC__
  42. void inputTest (WINDOW *);
  43. void scrollTest (WINDOW *);
  44. void introTest (WINDOW *);
  45. int initTest (WINDOW **);
  46. void outputTest (WINDOW *);
  47. void padTest (WINDOW *);
  48. void display_menu(int,int);
  49. #  ifdef PDCURSES
  50. void resizeTest (WINDOW *);
  51. #  endif
  52. #else
  53. void inputTest ();
  54. void scrollTest ();
  55. void introTest ();
  56. int initTest ();
  57. void outputTest ();
  58. void padTest ();
  59. void display_menu();
  60. #  ifdef PDCURSES
  61. void resizeTest ();
  62. #  endif
  63. #endif
  64.  
  65. struct commands
  66. {
  67.  char *text;
  68. #ifdef __STDC__
  69.  void (*function)(WINDOW *);
  70. #else
  71.  void (*function)();
  72. #endif
  73. };
  74. typedef struct commands COMMAND;
  75. #ifdef PDCURSES
  76. #define MAX_OPTIONS 6
  77. #else
  78. #define MAX_OPTIONS 5
  79. #endif
  80. COMMAND command[MAX_OPTIONS] =
  81. {
  82.  {"Intro Test",introTest},
  83.  {"Pad Test",padTest},
  84. #ifdef PDCURSES
  85.  {"Resize Test",resizeTest},
  86. #endif
  87.  {"Scroll Test",scrollTest},
  88.  {"Input Test",inputTest},
  89.  {"Output Test",outputTest}
  90. };
  91.  
  92. int     width, height;
  93.  
  94. int main()
  95. {
  96. WINDOW  *win;
  97. int key,old_option=(-1),new_option=0;
  98. bool quit=FALSE;
  99.  
  100. #ifdef PDCDEBUG
  101.     PDC_debug("testcurs started\n");
  102. #endif
  103.     if (initTest (&win)) {return 1;}
  104.  
  105. #ifdef A_COLOR
  106.     if (has_colors())
  107.       {
  108.        init_pair(1,COLOR_WHITE,COLOR_BLUE);
  109.        wbkgd(win, COLOR_PAIR(1));
  110.       }
  111.     else
  112.        wbkgd(win, A_REVERSE);
  113. #else
  114.     wbkgd(win, A_REVERSE);
  115. #endif
  116.  
  117.     erase();
  118.     display_menu(old_option,new_option);
  119.     while(1)
  120.       {
  121.        noecho();
  122.        keypad(stdscr,TRUE);
  123.        raw();
  124.        key = getch();
  125.        switch(key)
  126.          {
  127.           case 10:
  128.           case 13:
  129.           case KEY_ENTER:
  130.                          erase();
  131.                          refresh();
  132.                          (*command[new_option].function)(win);
  133.                          erase();
  134.                          display_menu(old_option,new_option);
  135.                          break;
  136.           case KEY_UP:
  137.                          new_option = (new_option == 0) ? new_option : new_option-1;
  138.                          display_menu(old_option,new_option);
  139.                          break;
  140.           case KEY_DOWN:
  141.                          new_option = (new_option == MAX_OPTIONS-1) ? new_option : new_option+1;
  142.                          display_menu(old_option,new_option);
  143.                          break;
  144.           case 'Q':
  145.           case 'q':
  146.                          quit = TRUE;
  147.                          break;
  148.           default:       break;
  149.          }
  150.        if (quit == TRUE)
  151.           break;
  152.       }
  153.  
  154.     delwin (win);
  155.     endwin();
  156.     return 0;
  157. }
  158. #ifdef __STDC__
  159. void Continue (WINDOW *win)
  160. #else
  161. void Continue (win)
  162. WINDOW *win;
  163. #endif
  164. {
  165.     wmove(win, 10, 1);
  166.     wclrtoeol(win);
  167.     mvwaddstr(win, 10, 1, " Press any key to continue");
  168.     wrefresh(win);
  169.     raw();
  170.     wgetch(win);
  171. }
  172.  
  173. #ifdef __STDC_
  174. int initTest (WINDOW **win)
  175. #else
  176. int initTest (win)
  177. WINDOW **win;
  178. #endif
  179. {
  180. #ifdef PDCDEBUG
  181.     PDC_debug("initTest called\n");
  182. #endif
  183.     initscr();
  184. #ifdef PDCDEBUG
  185.     PDC_debug("after initscr()\n");
  186. #endif
  187. #ifdef A_COLOR
  188.     if (has_colors())
  189.        start_color();
  190. #endif
  191.     width  = 60;
  192.     height = 13;                /* Create a drawing window */
  193.     *win = newwin(height, width, (LINES-height)/2, (COLS-width)/2);
  194.     if(*win == NULL)
  195.     {   endwin();
  196.         return 1;
  197.     }
  198.     return(0);
  199. }
  200. #ifdef __STDC__
  201. void introTest (WINDOW *win)
  202. #else
  203. void introTest (win)
  204. WINDOW *win;
  205. #endif
  206. {
  207.     wmove( win, height/2-5, width/2 );
  208.     wvline( win, ACS_VLINE, 10 );
  209.     wmove( win, height/2, width/2-10 );
  210.     whline( win, ACS_HLINE, 20 );
  211.     Continue(win);
  212.  
  213.     beep ();
  214.     werase(win);
  215.  
  216.     box(win, ACS_VLINE, ACS_HLINE);
  217.     wrefresh(win);
  218.     cbreak ();
  219.     mvwaddstr(win, 1, 1, "You should have rectangle in the middle of the screen");
  220.     mvwaddstr(win, 2, 1, "You should have heard a beep");
  221.     Continue(win);
  222.     return;
  223. }
  224. #ifdef __STDC__
  225. void scrollTest (WINDOW *win)
  226. #else
  227. void scrollTest (win)
  228. WINDOW *win;
  229. #endif
  230. {
  231.     int i;
  232.     int OldX, OldY;
  233.     char *Message = "The window will now scroll slowly";
  234.  
  235. /* disable typeahead checking */
  236.  
  237.     typeahead(-1);
  238.  
  239.     mvwprintw (win, height - 2, 1, Message);
  240.     wrefresh (win);
  241.     scrollok(win, TRUE);
  242.     for (i = 1; i <= height; i++) {
  243.       napms (150);
  244.       scroll(win);
  245.       wrefresh (win);
  246.     };
  247.  
  248.     getmaxyx (win, OldY, OldX);
  249.     mvwprintw (win, 6, 1, "The top of the window will scroll");
  250.     wmove (win, 1, 1);
  251.     wsetscrreg (win, 0, 4);
  252.     box(win, ACS_VLINE, ACS_HLINE);
  253.     wrefresh (win);
  254.     for (i = 1; i <= 5; i++) 
  255.       {
  256.        napms (500);
  257.        scroll(win);
  258.        wrefresh (win);
  259.       }
  260.  
  261.     mvwprintw (win, 3, 1, "The bottom of the window will scroll");
  262.     wmove (win, 8, 1);
  263.     wsetscrreg (win, 5, --OldY);
  264.     box(win, ACS_VLINE, ACS_HLINE);
  265.     wrefresh (win);
  266.     for (i = 5; i <= OldY; i++) 
  267.       {
  268.        napms (300);
  269.        wscrl(win,-1);
  270.        wrefresh (win);
  271.       }
  272.     wsetscrreg (win, 0, OldY);
  273.  
  274. }
  275. #ifdef __STDC__
  276. void inputTest (WINDOW *win)
  277. #else
  278. void inputTest (win)
  279. WINDOW *win;
  280. #endif
  281. {
  282.     int w, h, bx, by, sw, sh, i, c,num;
  283.     char buffer [80];
  284.     WINDOW *subWin;
  285.     wclear (win);
  286.  
  287.     getmaxyx(win, h, w);
  288.     getbegyx(win, by, bx);
  289.  
  290.     sw = w / 3;
  291.     sh = h / 3;
  292.     if((subWin = subwin(win, sh, sw, by + h - sh - 2, bx + w - sw - 2)) == NULL)
  293.         return;
  294.  
  295. #ifdef A_COLOR
  296.     if (has_colors())
  297.       {
  298.        init_pair(2,COLOR_WHITE,COLOR_RED);
  299.        wbkgd(subWin, COLOR_PAIR(2) | A_BOLD);
  300.       }
  301.     else
  302.        wbkgd(subWin, A_BOLD);
  303. #else
  304.     wbkgd(subWin, A_BOLD);
  305. #endif
  306.     box(subWin, ACS_VLINE, ACS_HLINE);
  307.     wrefresh(win);
  308.  
  309.     nocbreak();
  310.     mvwaddstr(win, 2, 1, "Press some keys for 5 seconds");
  311.     mvwaddstr(win, 1, 1, "Pressing ^C should do nothing");
  312.     wrefresh(win);
  313.  
  314.     werase (subWin);
  315.     box(subWin, ACS_VLINE, ACS_HLINE);
  316.     for (i = 0; i < 5; i++) 
  317.       {
  318.        mvwprintw (subWin, 1, 1, "Time = %d", i);
  319.        wrefresh(subWin);
  320.        napms(1000);
  321.        flushinp();
  322.       }
  323.  
  324.     delwin (subWin);
  325.     werase(win);
  326.     flash();
  327.     wrefresh(win);
  328.     napms(500);
  329.     flushinp();
  330.  
  331.     mvwaddstr(win, 2, 1, "Press a key, followed by ENTER");
  332.     wmove(win, 9, 10);
  333.     wrefresh(win);
  334.     echo();
  335.     noraw();
  336.     wgetnstr(win, buffer, 3);
  337.     flushinp();
  338.  
  339.     wmove(win, 9, 10);
  340.     wdelch(win);
  341.     mvwaddstr(win, 4, 1, "The character should now have been deleted");
  342.     Continue(win);
  343.  
  344.     wclear (win);
  345.     mvwaddstr(win, 2, 1, "Press a function key or an arrow key");
  346.     wrefresh(win);
  347.     keypad(win, TRUE);
  348.     raw();
  349.     c = wgetch(win);
  350.  
  351.     nodelay(win, TRUE);
  352.     wgetch(win);
  353.     nodelay(win, FALSE);
  354.  
  355.     refresh();
  356.     wclear (win);
  357.     mvwaddstr(win, 3, 2, "The window should have moved");
  358.     mvwaddstr(win, 4, 2, "This text should have appeared without you pressing a key");
  359.     mvwprintw(win, 2, 2, "Keycode = %d", c);
  360.     mvwaddstr(win, 6, 2, "Enter a number then a string seperated by space");
  361.     mvwin(win, 2, 1);
  362.     wrefresh(win);
  363.     mvwscanw(win, 7, 6, "%d %s", &num,buffer);
  364.     mvwprintw(win, 8, 6, "String: %s Number: %d", buffer,num);
  365.     Continue(win);
  366. }
  367. #ifdef __STDC__
  368. void outputTest (WINDOW *win)
  369. #else
  370. void outputTest (win)
  371. WINDOW *win;
  372. #endif
  373. {
  374.     WINDOW *win1;
  375.     char Buffer [80];
  376.     chtype ch;
  377.     int by, bx;
  378.  
  379.     nl ();
  380.     wclear (win);
  381.     mvwaddstr(win, 1, 1, "You should now have a screen in the upper left corner, and this text should have wrapped");
  382.     waddstr(win,"\nThis text should be down\n");
  383.     waddstr(win,  "and broken into two here ^");
  384.     Continue(win);
  385.  
  386.     wclear(win);
  387.     wattron(win, A_BOLD);
  388.     mvwaddstr(win, 1, 1, "A new window will appear with this text in it");
  389.     mvwaddstr(win, 8, 1, "Press any key to continue");
  390.     wrefresh(win);
  391.     wgetch(win);
  392.  
  393.     getbegyx(win, by, bx);
  394.  
  395.     win1 = newwin(10, 50, 15, 25);
  396.     if(win1 == NULL)
  397.     {   endwin();
  398.         return;
  399.     }
  400. #ifdef A_COLOR
  401.     if (has_colors())
  402.       {
  403.        init_pair(3,COLOR_BLUE,COLOR_WHITE);
  404.        wbkgd(win1, COLOR_PAIR(3));
  405.       }
  406.     else
  407.        wbkgd(win1, A_NORMAL);
  408. #else
  409.     wbkgd(win1, A_NORMAL);
  410. #endif
  411.     wclear (win1);
  412.     mvwaddstr(win1, 5, 1, "This text should appear; using overlay option");
  413.     copywin(win, win1,0,0,0,0,9,49,TRUE);
  414.  
  415. #if defined(PDCURSES) && !defined(XCURSES)
  416.     box(win1,0xb3,0xc4);
  417. #else
  418.     box(win1,ACS_VLINE,ACS_HLINE);
  419. #endif
  420.     wmove(win1, 8, 26);
  421.     wrefresh(win1);
  422.     wgetch(win1);
  423.  
  424.     wclear(win1);
  425.     wattron(win1, A_BLINK);
  426.     mvwaddstr(win1, 4, 1, "This blinking text should appear in only the second window");
  427.     wattroff(win1, A_BLINK);
  428.     mvwin(win1, by, bx);
  429.     overlay(win, win1);
  430.     mvwin(win1,15,25);
  431.     wmove(win1, 8, 26);
  432.     wrefresh(win1);
  433.     wgetch(win1);
  434.     delwin(win1);
  435.  
  436.     clear();
  437.     wclear(win);
  438.     wrefresh(win);
  439.     mvwaddstr(win, 6, 2, "This line shouldn't appear");
  440.     mvwaddstr(win, 4, 2, "Only half of the next line is visible");
  441.     mvwaddstr(win, 5, 2, "Only half of the next line is visible");
  442.     wmove(win, 6, 1);
  443.     wclrtobot (win);
  444.     wmove(win, 5, 20);
  445.     wclrtoeol (win);
  446.     mvwaddstr(win, 8, 2, "This line also shouldn't appear");
  447.     wmove(win, 8, 1);
  448.     winsdelln(win,-1);
  449.     Continue(win);
  450.  
  451.     wmove (win, 5, 9);
  452.     ch = winch (win);
  453.  
  454.     wclear(win);
  455.     wmove (win, 6, 2);
  456.     waddstr (win, "The next char should be l:  ");
  457.     winsch (win, ch);
  458.     Continue(win);
  459.  
  460.     mvwinsstr( win, 6, 2, "A1B2C3D4E5" );
  461.     Continue(win);
  462.  
  463.     wmove(win, 5, 1);
  464.     winsdelln (win,1);
  465.     mvwaddstr(win, 5, 2, "The lines below should have moved down");
  466.     Continue(win);
  467.  
  468.     wclear(win);
  469.     wmove(win, 2, 2);
  470.     wprintw(win, "This is a formatted string in a window: %d %s\n", 42, "is it");
  471.     mvwaddstr(win, 10, 1, "Enter a string: ");
  472.     wrefresh(win);
  473.     echo();
  474.     wscanw (win, "%s", Buffer);
  475.  
  476.     printw("This is a formatted string in stdscr: %d %s\n", 42, "is it");
  477.     mvaddstr(10, 1, "Enter a string: ");
  478.     scanw ("%s", Buffer);
  479.  
  480.     wclear(win);
  481.     curs_set(2);
  482.     mvwaddstr(win, 1, 1, "The cursor should appear as a block");
  483.     Continue(win);
  484.  
  485.     wclear(win);
  486.     curs_set(0);
  487.     mvwaddstr(win, 1, 1, "The cursor should have disappeared");
  488.     Continue(win);
  489.  
  490.     wclear(win);
  491.     curs_set(1);
  492.     mvwaddstr(win, 1, 1, "The cursor should be an underline");
  493.     Continue(win);
  494.  
  495. #ifdef A_COLOR
  496.     if ( has_colors() )
  497.       {
  498.        wclear(win);
  499.        mvwaddstr(win, 1, 1, "Colors should change after you press a key");
  500.        Continue(win);
  501.        init_pair(1, COLOR_RED, COLOR_WHITE);
  502.        wrefresh(win);
  503.     }
  504. #endif
  505.  
  506.     werase(win);
  507.     mvwaddstr(win, 1, 1, "Information About Your Terminal");
  508.     mvwaddstr(win, 3, 1, termname());
  509.     mvwaddstr(win, 4, 1, longname());
  510.     if ( termattrs() & A_BLINK )
  511.        mvwaddstr(win,5, 1, "This terminal supports blinking.");
  512.     else
  513.        mvwaddstr(win,5, 1, "This terminal does NOT support blinking.");
  514.  
  515.     mvwaddnstr( win, 7,5, "Have a nice day!ok", 16 );
  516.     wrefresh(win);
  517.  
  518.     mvwinnstr( win, 7,5, Buffer, 18 );
  519.     mvaddstr( LINES-2, 10, Buffer );
  520.     refresh();
  521.     Continue(win);
  522. }
  523.  
  524. #ifdef PDCURSES
  525. #ifdef __STDC__
  526. void resizeTest(WINDOW *dummy)
  527. #else
  528. void resizeTest(dummy)
  529. WINDOW *dummy;
  530. #endif
  531. {
  532.     WINDOW *win1;
  533.     char Buffer [80];
  534.     chtype ch;
  535.  
  536.     savetty ();
  537.  
  538.     clear();
  539.     refresh();
  540. #ifdef __PDCURSES__
  541.     resize_screen(50);
  542. #endif
  543.  
  544.  
  545.     win1 = newwin(11, 50, 14, 25);
  546.     if(win1 == NULL)
  547.     {   endwin();
  548.         return;
  549.     }
  550. #ifdef A_COLOR
  551.     if (has_colors())
  552.       {
  553.        init_pair(3,COLOR_BLUE,COLOR_WHITE);
  554.        wattrset(win1, COLOR_PAIR(3));
  555.       }
  556. #endif
  557.     wclear (win1);
  558.  
  559.     mvwaddstr(win1, 1, 1, "The screen may now have 50 lines");
  560.     Continue(win1);
  561.  
  562.     resetty ();
  563.  
  564.     wclear (win1);
  565.     mvwaddstr(win1, 1, 1, "The screen should now be reset");
  566.     Continue(win1);
  567.  
  568.     delwin(win1);
  569.  
  570.     clear();
  571.     refresh();
  572.  
  573. }
  574. #endif
  575.  
  576. #ifdef __STDC__
  577. void padTest(WINDOW *dummy)
  578. #else
  579. void padTest(dummy)
  580. WINDOW *dummy;
  581. #endif
  582. {
  583. WINDOW *pad,*spad;
  584.  
  585.  pad = newpad(50,100);
  586.  wattron(pad, A_REVERSE);
  587.  mvwaddstr(pad, 5, 2, "This is a new pad");
  588.  wattrset(pad,0);
  589.  mvwaddstr(pad, 8, 0, "The end of this line should be truncated here:except  now");
  590.  mvwaddstr(pad,11, 1, "This line should not appear.It will now");
  591.  wmove(pad, 10, 1);
  592.  wclrtoeol(pad);
  593.  mvwaddstr(pad, 10, 1, " Press any key to continue");
  594.  prefresh(pad,0,0,0,0,10,45);
  595.  keypad(pad, TRUE);
  596.  raw();
  597.  wgetch(pad);
  598.  
  599.  spad = subpad(pad,12,25,6,52);
  600.  mvwaddstr(spad, 2, 2, "This is a new subpad");
  601.  box(spad,0,0);
  602.  prefresh(pad,0,0,0,0,15,75);
  603.  keypad(pad, TRUE);
  604.  raw();
  605.  wgetch(pad);
  606.  
  607.  mvwaddstr(pad, 35, 2, "This is displayed at line 35 in the pad");
  608.  mvwaddstr(pad, 40, 1, " Press any key to continue");
  609.  prefresh(pad,30,0,0,0,10,45);
  610.  keypad(pad, TRUE);
  611.  raw();
  612.  wgetch(pad);
  613.  
  614.  delwin(pad);
  615. }
  616.  
  617. #ifdef __STDC__
  618. void display_menu(int old_option,int new_option)
  619. #else
  620. void display_menu(old_option,new_option)
  621. int old_option,new_option;
  622. #endif
  623. {
  624.  register int i;
  625.  
  626.  attrset(A_NORMAL);
  627.  mvaddstr(3,20,"PDCurses Test Program");
  628.  
  629.  for (i=0;i<MAX_OPTIONS;i++)
  630.     mvaddstr(5+i,25,command[i].text);
  631.  if (old_option != (-1))
  632.     mvaddstr(5+old_option,25,command[old_option].text);
  633.  attrset(A_REVERSE);
  634.  mvaddstr(5+new_option,25,command[new_option].text);
  635.  attrset(A_NORMAL);
  636.  mvaddstr(13,3,"Use Up and Down Arrows to select - Enter to run - Q to quit");
  637.  refresh();
  638. }
  639.  
  640.